package com.library.servlet;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.service.BookService;
import com.library.service.UserService;
import com.library.service.impl.BookServiceImpl;
import com.library.service.impl.UserServiceImpl;


public class LoginServlet extends HttpServlet{
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取 用户名和 密码
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		//根据业务的结果 进行页面跳转和数据添加
		UserService us= new UserServiceImpl();
		Map<String,Object> map = us.userLogin(username, password);
		if(map.get("loginUser") !=null) {
			//登录成功以后，会把登录的用户 存在session里,并且把查到的书 放在request里
			BookService bookService = new BookServiceImpl();
			request.getSession().setAttribute("loginUser", map.get("loginUser"));
			request.getRequestDispatcher("success.jsp").forward(request, response);
		}else {
			request.setAttribute("msg", map.get("msg"));
			request.getRequestDispatcher("login.jsp").forward(request, response);
		}
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.sendRedirect("success.jsp");
	}
	
}
